package com.j.lemon.learn.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author lijunjun
 * @Date 2019-03-19 15:52
 * @Description synchronized修饰普通方法，锁住的是当前对象，
 *              synchronized修饰静态方法，锁住的是类
 *              synchronized修饰静态方法效果等同于 synchronized(SynchronizedTest.class){}
 *                                        等同于 Object lock = new Object(); synchronized(lock)
 */
public class SynchronizedTest {
    private static ExecutorService executorService = Executors.newFixedThreadPool(10);
    private static final List<String> list = new ArrayList<>();
    private static synchronized void addIfPresent(){
            list.add("0");
    }

    static class STest implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                addIfPresent();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            executorService.submit(new STest());
        }
        Thread.sleep(1000);
        System.out.println(list.size());
        executorService.shutdown();
    }





}
